package com.woniuxy02;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author 作者
 * @create 2025-11-18 15:02
 */
public class Test01 {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Student.class;
        //返回所有公有属性
        Field[] fields = clazz.getFields();
        Stream.of(fields).forEach(System.out::println);

        //返回所有属性
        Field[] declaredFields = clazz.getDeclaredFields();
        Stream.of(declaredFields).forEach(System.out::println);
    }

    private static void test06() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        //反射可以操作私有成员
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        Field nameField = clazz.getDeclaredField("sname");
        System.out.println(nameField);
        //私有成员，设置暴力访问
        nameField.setAccessible(true);
        System.out.println(nameField.get(student));
        nameField.set(student, "zs");
        System.out.println(nameField.get(student));
    }

    private static void test05() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        //操作公有属性
        Field ageFiled = clazz.getField("age");
        //取值：传统的用法 对象.属性
        System.out.println(student.age);
        //取值：反射用法  字段.get(对象)
        System.out.println(ageFiled.get(student));
        //赋值：传统的用法 对象.属性=属性值;
//        student.age=11;
        //取值：反射用法  字段.set(对象，属性值)
        ageFiled.set(student,18);
        System.out.println(ageFiled.get(student));
    }

    private static void test04() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        //通过属性名，获取属性
        //getField(),只能获取公有字段
        Field fieldAge = clazz.getField("age");
        System.out.println(fieldAge);

        //getDeclaredField()可以获取任意访问修复的字段
        Field fieldSname = clazz.getDeclaredField("sname");
        System.out.println(fieldSname);
    }

    private static void test03() throws InstantiationException, IllegalAccessException {
        Class clazz = Student.class;
        //创建对象
        Student student = (Student) clazz.newInstance();
        System.out.println(student);
    }

    private static void test02() throws ClassNotFoundException {
        //获取二进制字节码对象
        //方法一：Class.forName("全限定名：包名+类名");
        Class clazz1 = Class.forName("com.woniuxy02.Student");
        //方法二：类型.class
        Class clazz2 = Student.class;
        //方法二：对象.getClass()
        Student student=new Student();
        Class  clazz3 = student.getClass();
        //反射Class对象,是单例的
        System.out.println(clazz1==clazz2);
        System.out.println(clazz1==clazz3);
    }

    private static void test01() {
        //传统的方法，创建对象，获取属性的值，给属性赋值，调用方法
        Student student=new Student(1,"zs");
        System.out.println(student.getSname());
        student.setSname("zxs");
        System.out.println(student.getSname());
        System.out.println(student.toString());
    }
}
